<!-- <script type="text/javascript" src="https://www.google.com/jsapi"></script> -->
<script type="text/javascript">
function removePost(){	
	var del_ID = document.getElementById("delID").value;	
	var f = document.createElement("form");
	f.setAttribute('method',"post");
	f.setAttribute('action',"/posts/delete/" + del_ID);
	f.submit();
}

function update(id){	
	document.location.href = "/posts/edit/" + id;
}

$(document).on("click", ".btnDel", function () {	
    var delID = $(this).data('id');
    $(".modal-body #delID").val( delID );
});

$(document).ready(function() { 
	$('table').tablesorter(); 
}); 	

google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(drawChart);

function drawChart() {
    var data = new google.visualization.DataTable(<?php echo $chartData;?>);
	
    var options = {
      title: 'Posts by Date',
      colors: ['#9b7188'],
      backgroundColor: '#eeeee6',
      width:250,
      height:200
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

<div class="row" style="padding-top: 40px;">
	<div class="bg-custom">
		
		<div class="row">
			<div class="col-md-4">
				<div align="center">
					<h1>
						<span class="text-left text-custom"><?php echo $user['Admin']['Username']?>'s Blog</span>
					</h1>
					<img
						src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTJfM-cz_DDwZpaT9SNHNSHJpMhdTbPVbM62bFPEDGREECUmcZptg"
						class="img-circle" style="max-height: 100px; max-width: 100px;">
				</div>
				<br>
				<!-- statistic -->
				<div class="col-md-offset-1">
					<p class="text-info">
						Fullname: <strong><?php echo $user['Admin']['Fullname']?></strong>
					</p>
					<p class="text-info">
						Posted: <strong><?php echo count($list)?></strong> post(s)
					</p>
					<p class="text-info">
						Last posted: <strong>
				<?php
				if ($list) {
					$total = max ( $list );
					echo $total ['Post'] ['created'];
				} else {
					echo 'This user has no post.';
				}
				?></strong>
					</p>
					<!-- graph -->
					<div id="chart_div" class=""></div>
				</div>
			</div>
			<div class="col-md-7">
				<div align="right">
					<ul class="pagination pagination-sm">
						<li><?php echo $this->Paginator->first('<<< First',array('class' => 'active'));?></li>
						<li><?php echo $this->Paginator->prev('<<', array('class' => 'active'), null, array('class'=>'hidden'));?></li>
						<li><?php echo $this->Paginator->numbers();?></li>
						<li><?php echo $this->Paginator->next('>>', array('class' => 'active'), null, array('class'=>'hidden'));?></li>
						<li><?php echo $this->Paginator->last('Last >>>',array('class' => 'active'));?></li>
					</ul>
				</div>
				<table class="table table-hover tablesorter">
					<thead>
						<th class="text-custom text-center"
							style="width: 80px; cursor: pointer;">No</th>
						<th class="text-custom"
							style="width: 400px; text-align: center; cursor: pointer;">Title</th>
						<?php if($this->Session->read('authen')):?>
						<th class="text-custom"
							style="width: 150px; text-align: center; cursor: pointer;">Actions</th>
						<?php endif;?>
						<th class="text-custom"
							style="width: 200px; text-align: center; cursor: pointer;">Last
							Updated</th>
					</thead>
    			<?php
							$no = ($this->Paginator->current ( 'Post' ) - 1) * $this->Session->read ( 'limit' );
							foreach ( $posts as $post ) :
								$no += 1;
								?>
		<tr>
						<td align="center"><?php echo $no;?></td>
						<td>
            		<?php
								echo $this->Html->link ( $post ['Post'] ['title'], array (
										'controller' => 'posts',
										'action' => 'view',
										$post ['Post'] ['id'],
										'?' => array (
												'b' => 'm',
												'a' => $user ['Admin'] ['AdminID'] 
										) 
								) );
								?>
        		</td>
        		<?php if($this->Session->read('authen')):?>
					<td style="text-align: center;">
							<button type="button" class="btn btn-default"
								onclick="update(<?php echo $post['Post']['id']; ?>)">
								<span class="glyphicon glyphicon-edit"></span>
							</button>

							<button type="button"
								data-id="<?php echo $post['Post']['id']; ?>"
								class="btn btn-c btnDel" data-toggle="modal"
								data-target=".bs-example-modal-sm">
								<span class="glyphicon glyphicon-remove"></span>
							</button>
						</td>
				<?php endif;?>
					<td style="text-align: center;"><?php echo $post['Post']['modified']; ?></td>
					</tr>
    			<?php endforeach; ?>
    			<?php unset($post); ?>
			</tbody>
				</table>
				<div align="right">
			<?php if($this->Session->read('authen')):?>
				<a href="/posts/add" class="btn btn-custom"
						style="width: 200px; color: #eeeee6;">Add new post</a>
			<?php endif;?>
			</div>
			</div>
		</div>
	</div>

</div>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog"
	aria-labelledby="myLargeModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">

			<div class="modal-header bg-custom">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
			</div>
			<div class="modal-body">
				Do you realy want to delete this record? <input type="hidden"
					id="delID" value="" />

			</div>
			<div class="modal-footer">
				<div class="btn-group btn-group-justified">
					<div class="btn-group">
						<button type="button" class="btn btn-inverse"
							onclick="removePost()">Delete</button>
					</div>
					<div class="btn-group">
						<button type="button" class="btn btn-custom" data-dismiss="modal">Close</button>
					</div>
				</div>

			</div>
		</div>
	</div>
</div>




